package com.golfbox.oa.code;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.EAN8Encoder;
import org.jbarcode.paint.EAN8TextPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;

/**
 * 支持EAN13, EAN8, UPCA, UPCE, Code 3 of 9, Codabar, Code 11, Code 93, Code 128,
 * MSI/Plessey, Interleaved 2 of PostNet等
 */
public class JBarcodeByEAN8Util {

    public void encodeByEAN8(String content, String imgPath, int with,
            int height) {
        try {
            JBarcode jBarcode = new JBarcode(EAN8Encoder.getInstance(),
                    WidthCodedPainter.getInstance(), EAN8TextPainter.getInstance());
            BufferedImage bufferedImage = jBarcode.createBarcode(content);
            FileOutputStream outputStream = new FileOutputStream(imgPath);
            ImageUtil.encodeAndWrite(bufferedImage, "jpeg", outputStream, with,height);
            outputStream.close();
            System.out.println("===");
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    public static void main(String[] paramArrayOfString) {
        JBarcodeByEAN8Util jBarCodeUtil = new JBarcodeByEAN8Util();
        jBarCodeUtil.encodeByEAN8("1101219", "D:/EAN8.jpg", 100, 70);
    }
}